//
//  guide.swift
//  firstDemo
//
//  Created by AINI on 2019/1/5.
//  Copyright © 2019 AINI. All rights reserved.
//

import UIKit

class Guide: UIViewController,UIScrollViewDelegate {
    var numOfPages = 3;
    let img = ["welcome1","welcome2","welcome3"]
    override func viewDidLoad() {
        super.viewDidLoad()
        let frame = self.view.bounds
        let scrollView = UIScrollView()
        scrollView.frame = self.view.bounds
        scrollView.delegate = self
        scrollView.contentSize = CGSize(width: frame.size.width*CGFloat(numOfPages), height: frame.size.height)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        for i in 0..<numOfPages{
            let image = UIImage(named:img[i])
            let imgView = UIImageView(image: image)
            imgView.frame = CGRect(x:frame.size.width*CGFloat(i), y:CGFloat(0),
                                   width:frame.size.width, height:frame.size.height)
            scrollView.addSubview(imgView)
        }
        scrollView.contentOffset = CGPoint.zero
        self.view.addSubview(scrollView)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let twidth = CGFloat(numOfPages-1) * self.view.bounds.size.width
        //如果在最后一个页面继续滑动的话就会跳转到主页面
        if scrollView.contentOffset.x > twidth {
            //            let mainStoryboard = UIStoryboard(name:"Main", bundle:nil)
            //            let viewController = mainStoryboard.instantiateInitialViewController()
            //           self.present(MainVC(), animated: true, completion: nil)
            let rootVC = UIApplication.shared.delegate as! AppDelegate
            rootVC.window?.rootViewController = ViewController()
        }
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
